package de.QuizCommunity;



import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Util;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

import de.QuizCommunity.maps.GPSTracker;
import de.QuizCommunity.service.Antwort_Put;
import de.QuizCommunity.service.Benutzer_XML_Parser;
import de.QuizCommunity.service.Frage_XML_Parser;
import de.QuizCommunity.util.AppVariabels;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import android.view.View;

@SuppressWarnings("unused")
public class Menue extends Activity {
	
	public static Facebook facebook = new Facebook("423770474339347");
    private SharedPreferences mPrefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menue);
        
        //Toast.makeText(this, "Hallo " +  AppVariabels.user.getVorname(), Toast.LENGTH_LONG).show();
        AppVariabels appVar = AppVariabels.getInstance();
        appVar.user = Benutzer_XML_Parser.getBenutzerByeMail("hans@gmx.de");
        try
        {
        	int mxFrgaeId=Antwort_Put.getMaxIdFrage();
        }catch(Exception e)
        {
        	AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(
					"Der Service ist nicht erreichbar!")
					.setCancelable(false)
					.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									Intent i = new Intent(Menue.this,Menue.class);
									startActivity(i);
									
								}
							}).show();
        	
        }
        for(int i =1; i<=Antwort_Put.getMaxIdFrage();i++)
        {
        	try
        	{
        		appVar.lstFrage.add(Frage_XML_Parser.getFrage(i));
        	}catch(Exception e)
        	{
        		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    			builder.setMessage(
    					"Der Service ist nicht erreichbar!")
    					.setCancelable(false)
    					.setPositiveButton("Ok",
    							new DialogInterface.OnClickListener() {
    								public void onClick(DialogInterface dialog,
    										int id) {
    									Intent i = new Intent(Menue.this,Menue.class);
    									startActivity(i);
    									
    								}
    							}).show();
        	}
        	
        
        }
    	mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        
       
        TextView stadtName = (TextView) findViewById(R.id.stadtName);
        stadtName.setText(appVar.getAktuellerStandort());


        final Button button = (Button) findViewById(R.id.btnLog);
    	
    	button.setOnClickListener(new View.OnClickListener() {
		
    		public void onClick(View v) {
    			SharedPreferences.Editor ed = mPrefs.edit();
    			ed.remove("access_token");
    			ed.commit();

    			AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    			mAsyncRunner.logout(getApplicationContext(), new RequestListener() {
    				  
    				  public void onComplete(String response, Object state) {}
    				  
    				  
    				  public void onIOException(IOException e, Object state) {}
    				  
    				  
    				  public void onFileNotFoundException(FileNotFoundException e,
    				        Object state) {}
    				  
    				  
    				  public void onMalformedURLException(MalformedURLException e,
    				        Object state) {}
    				  
    				  
    				  public void onFacebookError(FacebookError e, Object state) {}
    				});
    			
    			final Toast toastLogout= Toast.makeText(getApplicationContext(), "you logged out...",Toast.LENGTH_LONG);
    			toastLogout.show();
    			
    			Intent in = new Intent(Menue.this,Startseite.class);
    			startActivity(in);
    			
    		}
    		
    	});
    	
    	 final Button btnFrageErstellen = (Button) findViewById(R.id.BtnFrageErstellen);
     	
    	 btnFrageErstellen.setOnClickListener(new View.OnClickListener() {
 		
     		public void onClick(View v) {
     			Intent in = new Intent(Menue.this,Frage_Einstellen.class);
     			startActivity(in);		
     		}
     		
     	});
    	 
    	 final Button btnQuizStart = (Button) findViewById(R.id.BtnQuizStart);
      	
    	 btnQuizStart.setOnClickListener(new View.OnClickListener() {
 		
     		public void onClick(View v) {
     			Intent in = new Intent(Menue.this,Frage.class);
     			startActivity(in);		
     		}
     		
     	});
    	 
    	 final Button btnBewertungAnsehen = (Button) findViewById(R.id.btnBewertAnsehen);
       	
    	 btnBewertungAnsehen.setOnClickListener(new View.OnClickListener() {
 		
     		public void onClick(View v) {
     			Intent in = new Intent(Menue.this,Bewertung_Ansehen.class);
     			startActivity(in);		
     		}
     		
     	});
        
    }
}
